const axios = require('axios');

// 设置基础URL
const baseURL = 'http://localhost:3000/api/users';
const api = axios.create({ baseURL });

async function testUsers() {
  let userId;
  
  try {
    // 测试获取所有用户
    const usersResponse = await api.get('/');
    console.log('All users:', usersResponse.data);

    // 测试创建用户
    const createUserResponse = await api.post('/', {
      username: 'testuser',
      password_hash: 'hashed_password',
      nickname: 'Test User',
      avatar_url: 'http://example.com/avatar.jpg',
      openid: 'openid123',
      unionid: 'unionid456'
    });
    console.log('Created user:', createUserResponse.data);
    userId = createUserResponse.data.id;

    // 测试根据ID获取用户
    const userResponse = await api.get(`/${userId}`);
    console.log('User by ID:', userResponse.data);

    // 测试更新用户
    const updateUserResponse = await api.put(`/${userId}`, {
      username: 'testuser',
      password_hash: 'hashed_password',
      nickname: 'Updated Test User',
      avatar_url: 'http://example.com/avatar.jpg',
      openid: 'openid123',
      unionid: 'unionid456'
    });
    console.log('Updated user:', updateUserResponse.data);

    // 测试删除用户
    const deleteResponse = await api.delete(`/${userId}`);
    console.log('Delete user response status:', deleteResponse.status);

  } catch (err) {
    console.error('Test error:', err.response ? err.response.data : err.message);
  }
}

testUsers();
